package com.accompany.controller;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.lang.generator.UUIDGenerator;
import com.accompany.common.Constant;
import com.accompany.common.result.CommonResult;
import com.accompany.common.utils.RedisUtil;
import com.accompany.common.utils.UtilsTime;
import com.accompany.model.User;
import com.accompany.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lfs
 * @since 2023-03-08
 */
@Api(tags = {"用户接口"})
@RestController
@RequestMapping("/user")
public class UserController {


    WxMaService wxMaService;

    @Autowired
    UserService service;
    @Autowired
    RedisUtil redisUtil;

    @ApiOperation("微信使用手机号码登录")
    @GetMapping("/loginApp")
    public CommonResult phone(String sessionKey, String encryptedData, String iv) {
//        if (!wxMaService.switchover(appid)) {
//            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", appid));
//        }

         //用户信息校验
//        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
//            WxMaConfigHolder.remove();//清理ThreadLocal
//            return "user check failed";
//        }

        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        WxMaConfigHolder.remove();//清理ThreadLocal
        String phone = phoneNoInfo.getPhoneNumber();
        User user = service.getUserByPhone(phone);

        Map<String,Object> map = new HashMap<String,Object>();
        if(user==null){
            user = new User();
            user.setPhone(phone);
            user.setCerateTime(UtilsTime.getDayTime());
            boolean insert = service.save(user);
            if(!insert){
                return CommonResult.failed("保存用户失败");
            }
        }
        String token = UUID.randomUUID().toString().replace("-","");
        redisUtil.set(Constant.REDIS_USER_PREFIX+token,user);
        return CommonResult.success(user,token);
    }


    @ApiOperation("申请成为陪诊员")
    @PostMapping("/accompanyApply")
    public CommonResult accompanyApply(User userDTO){
        User user = service.getUserByPhone(userDTO.getPhone());
        if(user!=null && user.getIsCort() == "1"){
            return CommonResult.failed("你已经是陪诊员了，无需再次申请");
        }
        boolean insert = service.save(userDTO);
        if(insert) {
            return CommonResult.success("申请成功");
        }else {
            return CommonResult.failed("保存失败");
        }
    }

}
